def set_stage():
""" Sets up the stage for the game """
stage.set_background("soccerfield")
stage.disable_floor()
def add_player():
""" Adds a player to the stage for the user to control """
player = codesters.Sprite("player1")
player.go_to(0, -155)
return player
def add_ball():
""" Adds a ball to the stage and sets its attributes """
ball = codesters.Sprite("soccerball")
ball.set_y_speed(-8)
def collision(sprite, hit_sprite):
sprite.go_to(0, 0)
hit_sprite.hide()
# add any other actions...
def main():
""" Sets up the program and calls other functions """
set_stage()
player = add_player()
add_ball()
main()
t = codesters.Teacher()
defs = t.find_block("def")
collision_event_handlers = t.find_text("event_collision")
ignore_defs = ['set_stage', 'main', 'add_player', 'add_ball', 'collision']
def get_above_def(line_number, def_list):
""" Returns the name of the nearest function and the distance above the given line number """
function_name = ""
distance = line_number - def_list[0][0]
for func in def_list:
if line_number - func[0] <= distance and line_number - func[0] > 0:
function_name = func[1]
distance = line_number - func[0]
return function_name, distance
def find_new_function(ignore_list, def_list):
""" Returns tuples of (line_number, function def) for any functions that aren't specified in ignore list """
return_list = []
for d in def_list:
count = 0
for i in ignore_list:
if i in d[1]:
break
else:
count += 1
if count == len(ignore_list):
return_list.append((d[0],d[1]))
return return_list
try:
new_functions = find_new_function(ignore_defs, defs)
def_name = new_functions[0][1]
def_line_num = new_functions[0][0]
def_indent = t.get_indent_at_line(def_line_num)
except:
def_name = "DNE"
def_line_num = "DNE"
def_indent = "DNE"
try:
tval2 = collision_event_handlers[0][1]
tval2_line_num = collision_event_handlers[0][0]
tval2_indent = t.get_indent_at_line(tval2_line_num)
except:
tval2 = "DNE"
tval2_line_num = "DNE"
tval2_indent = "DNE"
try:
above_def, distance = get_above_def(tval2_line_num, defs)
except:
above_def = "DNE"
distance = "DNE"
t1 = TestObjective()
t1.add_success("head_ball" in def_name, "Great job!")
t1.add_failure(def_name == "DNE", "Did you change the name of collision() to head_ball()?")
t2 = TestObjective()
t2.add_success("player." in tval2 and "(head_ball)" in tval2, "Great job!")
t2.add_failure(tval2 == "DNE", "Did you add the Collision Event Handler to the main() function?")
t2.add_failure("player." not in tval2, "Did you change sprite to player in front of the event handler?")
t2.add_failure("(head_ball)" not in tval2, "Did you change collision to head_ball inside the parentheses?")
t3 = TestObjective()
t3.add_success("main" in above_def and def_indent == 4, "Great job!")
t3.add_failure("main" not in above_def, "Did you place Collision Event Handler within main()?")
t3.add_failure(def_indent < 0 or def_indent > 4, "Make sure that Collision Event Handler is indented once within main()!")
tester = TestManager()
tester.add_test_list([t1, t2, t3])
tester.run_tests()
tester.display_first_feedback()
-
Run Code
-
Activity Submitted!
Enviar Trabajo
-
Actividad Siguiente
-
Stop Running Code
-
Show Chart
-
Show Console
-
Reset Code Editor
-
Codesters How To (opens in a new tab)